package com.yanqu.road.server.cmd.activity.peakseacraft;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PeakSeacraftProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.protocol.*;

/**
 * @Description
 */
@Cmd(code = GameProtocol.S_PEAK_SEACRAFT_GET_UNION_RANK_LIST, desc = "海上争霸 南海丝路 查看排名榜")
public class PeakSeacraftGetUnionRankListCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        PeakSeacraftProto.PeakSeacraftGetUnionRankListReqMsg reqMsg = PeakSeacraftProto.PeakSeacraftGetUnionRankListReqMsg.parseFrom(packet.getBytes());
        PeakSeacraftProto.PeakSeacraftGetUnionRankListGameToCrossMsg.Builder gameToCrossMsg = PeakSeacraftProto.PeakSeacraftGetUnionRankListGameToCrossMsg.newBuilder();
        gameToCrossMsg.setActivityId(reqMsg.getActivityId());
        gameToCrossMsg.setUnionId(player.getUserInfo().getUnionUid());
        gameToCrossMsg.setCtsMsg(reqMsg);

        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        if (null == activityInfo) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (activityInfo.getType() == eActivityType.PeakSeacraftActivity.getValue()) {
            ret = PeakSeacraftMgr.canEntryActivity(player,reqMsg.getActivityId());
            if(ret != 0){
                return;
            }

            player.sendUnionActivityGroupPacket(CrossProtocol.C_PEAK_SEACRAFT_GET_UNION_RANK_LIST, gameToCrossMsg, gameToCrossMsg.getActivityId());
        } else {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            PeakSeacraftProto.PeakSeacraftGetUnionRankListRespMsg.Builder resp = PeakSeacraftProto.PeakSeacraftGetUnionRankListRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_PEAK_SEACRAFT_GET_UNION_RANK_LIST,resp);
        }
    }
}
